import api from '@/api';

export default {
  namespace: 'org',
  state: {
    orgTree: [],
    orgType: [],
    orgUsers: [],
  },

  effects: {
    *create({ payload }, { call, put }) {
      return yield call(api.org.create, payload);
    },
    *update({ payload }, { call, put }) {
      return yield call(api.org.update, payload);
    },
    *delete({ payload }, { call, put }) {
      return yield call(api.org.delete, payload);
    },
    *queryOrgTree({ payload }, { call, put }) {
      const response = yield call(api.org.queryOrgTree, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveOrgTree',
          payload: response.data,
        });
      }
    },
    *queryOrgType({ payload }, { call, put }) {
      const response = yield call(api.org.queryOrgType, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveOrgType',
          payload: response.data,
        });
      }
    },
    *queryOrgInfo({ payload }, { call, put }) {
      return yield call(api.org.queryOrgInfo, payload);
    },
    *fetchOrgAllUsers({ payload }, { call, put }) {
      const response = yield call(api.org.queryOrgAllUsers, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveOrgUsers',
          payload: response.data,
        });
      }
    },
  },

  reducers: {
    saveOrgTree(state, { payload }) {
      return {
        ...state,
        orgTree: payload,
      };
    },
    saveOrgType(state, { payload }) {
      return {
        ...state,
        orgType: payload,
      };
    },
    saveOrgUsers(state, { payload }) {
      return {
        ...state,
        orgUsers: payload,
      };
    },
  },
};
